package com.roc.steelend.service.Impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.roc.steelend.controller.vo.reconciliation.FindReconciliationReception;
import com.roc.steelend.controller.vo.reconciliation.ReconciliationSaleReception;
import com.roc.steelend.controller.vo.returnformat.ReturnMsg;
import com.roc.steelend.mapper.ClientMapper;
import com.roc.steelend.mapper.ReconciliationSaleMapper;
import com.roc.steelend.mapper.SaleOrderMapper;
import com.roc.steelend.pojo.Client;
import com.roc.steelend.pojo.ReconciliationBuy;
import com.roc.steelend.pojo.ReconciliationSale;
import com.roc.steelend.service.ReconciliationSaleService;
import com.roc.steelend.util.LshUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigInteger;
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

@Service
@Transactional(rollbackFor = Exception.class)
public class ReconciliationSaleServiceImpl  implements ReconciliationSaleService {

    @Autowired
    private ReconciliationSaleMapper reconciliationSaleMapper;
    @Autowired
    private SaleOrderMapper saleOrderMapper;
    @Autowired
    private ClientMapper clientMapper;

    @Override
    public ReturnMsg postReconciliationSale(ReconciliationSaleReception reconciliationSaleReception, Integer employeeId) {
//        生成调账的数据销售
        ReturnMsg msg = new ReturnMsg();
        ReconciliationSale reconciliationSale = new ReconciliationSale();
        BigInteger reconciliationSaleLsh = LshUtil.getReconciliationSale();
        reconciliationSale.setReconciliationSaleId(reconciliationSaleLsh);
        reconciliationSale.setReconciliationSaleTime(reconciliationSaleReception.getReconciliationTime());
        reconciliationSale.setOperateTime(new Timestamp(new Date().getTime()));
        reconciliationSale.setEmployeeId(employeeId);
        reconciliationSale.setClientId(reconciliationSaleReception.getClientId());
        reconciliationSale.setOriginallyMoney(reconciliationSaleReception.getOldDebt());
        reconciliationSale.setNowMoney(reconciliationSaleReception.getNewDebt());
        if (Objects.equals(reconciliationSaleReception.getRemark(), "")){
            reconciliationSaleReception.setRemark(null);
        }
        reconciliationSale.setRemark(reconciliationSaleReception.getRemark());
        int insert = reconciliationSaleMapper.insert(reconciliationSale);
        if (insert==0){
            msg.setMsg("调账操作失败，请重试");
            msg.setCode(0);
            return  msg;
        }
//        更新客户的金额数据
        QueryWrapper<Client> clientQueryWrapper = new QueryWrapper<>();
        clientQueryWrapper.eq("client_id",reconciliationSaleReception.getClientId());
        Client client = clientMapper.selectList(clientQueryWrapper).get(0);
        client.setDebt(reconciliationSaleReception.getNewDebt());
        clientMapper.updateById(client);

//        操作销售订单
            saleOrderMapper.updateReconciliationByClient(reconciliationSaleReception.getClientId(),reconciliationSaleLsh);


        msg.setMsg("调账操作成功");
        msg.setCode(1);
        return msg;
    }

    @Override
    public List<HashMap<String, Object>> findReconciliationDataBySale(FindReconciliationReception findReconciliationReception) {
        Integer now = findReconciliationReception.getCurrentPage();
        Integer size = findReconciliationReception.getPageSize();
        now=(now-1)*size;
        String duiXiang = findReconciliationReception.getDuiXiang();
        if (Objects.equals(duiXiang, "")||duiXiang==null){
            duiXiang=null;
        }else {
            duiXiang="%"+duiXiang+"%";
        }

        return  reconciliationSaleMapper.findReconciliationDataBySale(now,size,duiXiang);
    }

    @Override
    public Integer findReconciliationDataBySaleCount(FindReconciliationReception findReconciliationReception) {
        String duiXiang = findReconciliationReception.getDuiXiang();

        if (Objects.equals(duiXiang, "")||duiXiang==null){
            duiXiang=null;
        }else {
            duiXiang="%"+duiXiang+"%";
        }
        return reconciliationSaleMapper.findReconciliationDataBySaleCount(duiXiang);
    }

    @Override
    public List<ReconciliationSale> saleReconciliationDetailSelect(BigInteger reconciliationId) {
        QueryWrapper<ReconciliationSale> reconciliationSaleQueryWrapper = new QueryWrapper<>();
        reconciliationSaleQueryWrapper.eq("reconciliation_sale_id",reconciliationId);
        return       reconciliationSaleMapper.selectList(reconciliationSaleQueryWrapper);
    }
}
